package com.iss.apartmentmanage.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iss.apartmentmanage.entity.User;
import com.iss.apartmentmanage.entity.UserInfo;
import com.iss.apartmentmanage.model.GetForArrays;
import com.iss.apartmentmanage.model.MyResponseData;
import com.iss.apartmentmanage.model.BaseModel;
import com.iss.apartmentmanage.model.UserWithUserInfo;
import com.iss.apartmentmanage.service.impl.UserInfoServiceImpl;
import com.iss.apartmentmanage.service.impl.UserServiceImpl;
import com.iss.apartmentmanage.utils.MyJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/employee")
public class UserInfoController {
    @Autowired
    UserInfoServiceImpl userInfoService;
    @Autowired
    UserServiceImpl userService;

    /**
     * 员工管理分页查询+条件查询
     * @param pageNum
     * @param pageSize
     * @param baseModel
     * @return
     */
    @RequestMapping(value = "/selectAllUserInfoMeassage",method = RequestMethod.POST)
    public String selectAllUserInfoMeassage(@RequestParam int pageNum, @RequestParam int pageSize,@RequestBody BaseModel baseModel){
            IPage<UserInfo> iPage = userInfoService.selectAllUserInfoMessage(pageNum,pageSize, baseModel);
            MyResponseData myResponseData = new MyResponseData();
            if(iPage!=null) {
                myResponseData.setObject(iPage);
                myResponseData.setErrorCode(1);
                myResponseData.setStatus("查询成功");
            }else {
                myResponseData.setErrorCode(0);
                myResponseData.setStatus("查询失败");
            }
            return MyJson.getJson(myResponseData);
    }

    /**
     * 员工管理插入
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/insertUserInfo",method = RequestMethod.POST)
    public MyResponseData insertUserInfo(@RequestBody UserInfo userInfo){
        int count = 0;
        MyResponseData myResponseData = new MyResponseData();
        try {
            if (userInfo != null) {
                count = userInfoService.insertUserInfo(userInfo);
                if (count > 0) {
                    myResponseData.setErrorCode(1);
                    myResponseData.setMsg("插入成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("插入有点问题");
        }
        return myResponseData;
    }

    /**
     * 根据员工信息id
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/updateUserInfoById",method = RequestMethod.POST)
    public MyResponseData updateUserInfoById( UserInfo userInfo){
        MyResponseData myResponseData = new MyResponseData();
        if(userInfo!=null){
           int count = userInfoService.updateUserInfoById(userInfo);
            if(count>0){
                myResponseData.setErrorCode(1);
                myResponseData.setMsg("更新成功");
            }
        }
        return myResponseData;
    }
    @RequestMapping(value = "/deleteUserInfoById",method = RequestMethod.POST)
    public MyResponseData deleteUserInfoById(@RequestParam Integer id){
        System.out.println("id========="+id);
        MyResponseData myResponseData = new MyResponseData();
        if(id!=null){
            int count  = userInfoService.deleteUserInfoById(id);
            if(count>0){
                myResponseData.setErrorCode(1);
                myResponseData.setMsg("删除成功");
            }
        }
        return myResponseData;
    }
    @RequestMapping(value = "/deleteUserInfoByBatchIds",method = RequestMethod.POST)
    public MyResponseData deleteUserInfoByBatchIds(@RequestBody GetForArrays arr){
        MyResponseData myResponseData = new MyResponseData();
        if(arr!=null){
            int count  = userInfoService.deleteUserInfoByBatchIds(arr.getId());
            if (count>0){
                myResponseData.setErrorCode(1);
                myResponseData.setMsg("删除多条数据成功");
            }
        }
        return myResponseData;
    }
    /**
     * 先根据用户名 查询userinfo整条信息
     * @param username
     * @return
     */
    @RequestMapping(value = "/getUserinfoByUsername",method = RequestMethod.POST)
    public MyResponseData getUserinfoByUsername (@RequestParam String username){
        MyResponseData myResponseData = new MyResponseData();
        UserWithUserInfo userWithUserInfo=userInfoService.getUserinfoByUsername(username);
        if (userWithUserInfo!=null){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");
            myResponseData.setObject(userWithUserInfo);
        }
        return  myResponseData;
    }

    @PostMapping("/updateIsUseByUserinfoId")
    public MyResponseData updateIsUseByUserinfoId(@RequestBody UserInfo userInfo ){
        MyResponseData myResponseData = new MyResponseData();
        int count= userInfoService.updateUserInfoById(userInfo);
        if (count!=0){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");

        }
        return myResponseData;
    }

    @PostMapping("/updateIsUseByUinfoIds")
    public MyResponseData updateIsUseByUinfoIds(@RequestBody GetForArrays getForArrays){
        MyResponseData myResponseData = new MyResponseData();
        int count= userInfoService.updateIsUsedByUinfoIds(getForArrays.getId(), getForArrays.getIsUsed());
        if (count!=0){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");

        }
        return myResponseData;
    }

    @RequestMapping(value = "/getUserInfoUnameAndUpwdByUid",method = RequestMethod.POST)
    public MyResponseData getUserInfoUnameAndUpwdByUid(@RequestBody UserWithUserInfo userinfoId){
        System.out.println("-------userinfoId=="+userinfoId);
        MyResponseData myResponseData = new MyResponseData();
        UserWithUserInfo userWithUserInfo = new UserWithUserInfo();
        userWithUserInfo.setId(userinfoId.getUserinfoId());
        UserWithUserInfo userInfoUnameAndUpwdByUid = userInfoService.getUserInfoUnameAndUpwdByUid(userWithUserInfo);
        System.out.println("========"+userInfoUnameAndUpwdByUid);
        try {
            if (userInfoUnameAndUpwdByUid != null) {
                myResponseData.setObject(userInfoUnameAndUpwdByUid);
                myResponseData.setErrorCode(1);
                myResponseData.setMsg("查询成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("error");
        }
        return myResponseData;
    }

    /**
     * 为员工申请系统账号和密码
     * @param user
     * @param id
     * @return
     */
    @RequestMapping(value = "/registerUNameAndUPwdForEmployee",method = RequestMethod.POST)
    public MyResponseData registerUNameAndUPwdForEmployee(@RequestBody User user,@RequestParam Integer id){
        MyResponseData myResponseData = new MyResponseData();
        UserInfo userInfo = new UserInfo();
        Integer count1 = userService.insertUser(user);
        if(count1!=0){
            myResponseData.setMsg("插入成功");
            myResponseData.setErrorCode(1);
        }
        Integer userinfoId = userService.getUserIdByUserName(user);
        userInfo.setUserinfoId(userinfoId);
        userInfo.setId(id);
        Integer count = userInfoService.updateUserInfoIdByUser(userInfo);
        if(count!=0) {
            myResponseData.setMsg("申请账号成功");
            myResponseData.setErrorCode(1);
        }
        return myResponseData;
    }
}
